function ajax(ops = {}){
    let {data={}, type="get", url, timeout=500} = ops;
    let str = "";
    for(let i in data){
        str += `${i}=${data[i]}&`;
    }
    if(type === "get"){
        url += "?" + str.slice(0, -1);
    }
    const xhr = new XMLHttpRequest();
    xhr.open(type, url);
    if(type === "post"){
        xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xhr.send(str.slice(0,-1));
    }else{
        xhr.send();
    }
    return new Promise((resolve,reject)=>{
        xhr.onreadystatechange = function(){
            if(xhr.readyState === 4 && xhr.status === 200){
                resolve(xhr.responseText);
            }else if(xhr.readyState === 4 && xhr.status !== 200){
                reject(xhr.status)
            }
        }
        setTimeout(()=>{
            reject("timeout");
        }, timeout);
    })
}



